import json

port_list = ["21311", "21312", "21313", "21314"]
new_port = []
port_dict = {"data": None}
for port in port_list:
    pdict = {}
    pdict["{#TCP_PORT}"] = port
    new_port.append(pdict)
    port_dict["data"] = new_port
    jsonStr = json.dumps(port_dict, sort_keys=True, indent=4)

print(jsonStr)

# zabbix_agentd.conf
# UserParameter=middle.discovery,python /usr/local/zabbix/etc/zabbix_agentd.conf.d/shell/port_monitor.py